package com.wangyh.edit.prettyedit.tree;

import com.wangyh.edit.prettyedit.constant.EditConstant;
import com.wangyh.edit.prettyedit.pages.PageFactory;
import javafx.scene.Node;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.File;

public class TreeClick {

    /**
     * 点击事件，不同的文件类型，进行不同的处理。
     *
     * @param file
     * @param tree
     */
    public static void clickNode(File file, TreeView<File> tree){
        //设置
        if(file.getName().endsWith("txt")||file.getName().endsWith("json")||file.getName().endsWith("xml")
                ||file.getName().endsWith("conf")||file.getName().endsWith("sh")||file.getName().endsWith("yml")){
            Node text = PageFactory.createPageService("txt").generatePage(file.getAbsolutePath());
            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
            borderPane.setCenter(text);
        }else if(file.getName().endsWith("html")){
            Node text = PageFactory.createPageService("html").generatePage(file.getAbsolutePath());
            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
            borderPane.setCenter(text);
        }else if(file.getName().endsWith("md")){
            Node text = PageFactory.createPageService("markdown").generatePage(file.getAbsolutePath());
            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
            borderPane.setCenter(text);
        }else if(file.getName().endsWith("flow")){
            Node text = PageFactory.createPageService("flow").generatePage(file.getAbsolutePath());
            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
            borderPane.setCenter(text);
        }else if(file.isDirectory()){
            Node page = PageFactory.createPageService("fileList").generatePage(file.getAbsolutePath());
            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
            borderPane.setCenter(page);
        }else {
            //打开文件概览
            Node page = PageFactory.createPageService("fileDesc").generatePage(file.getAbsolutePath());
            BorderPane borderPane = (BorderPane) tree.getScene().getRoot();
            borderPane.setCenter(page);
        }

        //设置名字
        Stage stage = (Stage) tree.getScene().getWindow();
        if(!file.isDirectory()){
            stage.setTitle(EditConstant.appName + "   //-> " + file.getName());
        }else {
            stage.setTitle(EditConstant.appName + "   //-> " + file.getAbsolutePath());
        }
    }
}
